//
//  File.swift
//  Documentation
//
//  Created by Toj on 6/19/23.
//

import Foundation

/* CoreGraphics - CGBitmapContext.h
   Copyright (c) 2000-2011 Apple Inc.
   All rights reserved. */

/* The callback for releasing the data supplied to
   `CGBitmapContextCreateWithData'. */

public typealias CGBitmapContextReleaseDataCallback = @convention(c) (UnsafeMutableRawPointer?, UnsafeMutableRawPointer?) -> Void
extension CGContext {

    
    /* Create a bitmap context. The context draws into a bitmap which is `width'
       pixels wide and `height' pixels high. The number of components for each
       pixel is specified by `space', which may also specify a destination color
       profile. Note that the only legal case when `space' can be NULL is when
       alpha is specified as kCGImageAlphaOnly. The number of bits for each
       component of a pixel is specified by `bitsPerComponent'. The number of bytes
       per pixel is equal to `(bitsPerComponent * number of components + 7)/8'.
       Each row of the bitmap consists of `bytesPerRow' bytes, which must be at
       least `width * bytes per pixel' bytes; in addition, `bytesPerRow' must be
       an integer multiple of the number of bytes per pixel. `data', if non-NULL,
       points to a block of memory at least `bytesPerRow * height' bytes. If `data'
       is NULL, the context will allocate the data itself; this data will be freed
       when the context is deallocated. `bitmapInfo' specifies whether the bitmap
       should contain an alpha channel and how it's to be generated, along with
       whether the components are floating-point or integer. If `releaseCallback' is
       non-NULL, it is called when the context is freed with `releaseInfo' and
       `data' as arguments. */
    
    @available(iOS 4.0, *)
    public /*not inherited*/ init?(data: UnsafeMutableRawPointer?, width: Int, height: Int, bitsPerComponent: Int, bytesPerRow: Int, space: CGColorSpace, bitmapInfo: UInt32, releaseCallback: CGBitmapContextReleaseDataCallback?, releaseInfo: UnsafeMutableRawPointer?)

    /** 创建位图上下文
     * 强制解压缩的原理就是对图片进行重新绘制，得到一张新的解压缩后的位图
     * 1. data:
     ** 如果不为 NULL: 那么它应该指向一块大小至少为 (bytesPerRow x height) 字节的内存;
     ** 如果是为 NULL: 那么系统就会为我们自动分配和释放所需的内存,
     ** 所以一般指定 NULL 即可;
     * 2. width 和height: 位图的宽度和高度, 分别赋值为图片的像素宽度和像素高度即可;
     * 3. bitsPerComponent: 像素的每个颜色分量使用的 bit 数, 在 RGB 颜色空间下指定 8 即可;
     * 4. bytesPerRow: 位图的每一行使用的字节数, 大小至少为 width x bytes per pixel 字节.
     ** 当我们指定 0/NULL 时, 系统不仅会为我们自动计算, 而且还会进行 cache line alignment 的优化
     * 5. space: 就是我们前面提到的颜色空间, 一般使用 RGB 即可;
     * 6. bitmapInfo: 位图的布局信息. kCGImageAlphaPremultipliedFirst
     */
    public /*not inherited*/ init?(data: UnsafeMutableRawPointer?, width: Int, height: Int, bitsPerComponent: Int, bytesPerRow: Int, space: CGColorSpace, bitmapInfo: UInt32)

    
    /* Return the data associated with the bitmap context `context', or NULL if
       `context' is not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var data: UnsafeMutableRawPointer? { get }

    
    /* Return the width of the bitmap context `context', or 0 if `context' is
       not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var width: Int { get }

    
    /* Return the height of the bitmap context `context', or 0 if `context' is
       not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var height: Int { get }

    
    /* Return the bits per component of the bitmap context `context', or 0 if
       `context' is not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var bitsPerComponent: Int { get }

    
    /* Return the bits per pixel of the bitmap context `context', or 0 if
       `context' is not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var bitsPerPixel: Int { get }

    
    /* Return the bytes per row of the bitmap context `context', or 0 if
       `context' is not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var bytesPerRow: Int { get }

    
    /* Return the color space of the bitmap context `context', or NULL if
       `context' is not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var colorSpace: CGColorSpace? { get }

    
    /* Return the alpha info of the bitmap context `context', or
       "kCGImageAlphaNone" if `context' is not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var alphaInfo: CGImageAlphaInfo { get }

    
    /* Return the bitmap info of the bitmap context `context', or 0 if `context'
       is not a bitmap context. */
    
    @available(iOS 2.0, *)
    public var bitmapInfo: CGBitmapInfo { get }

    
    /* Return an image containing a snapshot of the bitmap context `context'. If
       context is not a bitmap context, or if the image cannot be created for
       any reason, this function returns NULL. This is a "copy" operation ---
       subsequent changes to context will not affect the contents of the
       returned image.
    
       Note that in some cases the copy will actually follow "copy-on-write"
       semantics, so that the actual physical copy of the bits will only occur
       if the underlying data in the bitmap context is modified. As a
       consequence, you may wish to use the resulting image and release it
       before performing more drawing into the bitmap context; in this way, the
       actual physical copy of the data may be avoided. */
    
    @available(iOS 2.0, *)
    public func makeImage() -> CGImage?
}
